package a03_哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * a03_快乐数
 * </p>
 *
 * @author flyduck
 * @since 2024-06-24
 */
public class a04_快乐数复习4 {

    public static void main(String[] args) {
//        int n = 129;

        System.out.println(isHappy(19));
/*
        System.out.println(getSquareSum(n));
*/
//        System.out.println(getNext(n));
    }

    public static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true) {
            if (n == 1) {
                return true;
            }
            n = getNext(n);
            if (set.contains(n)) {
                return false;
            }
            set.add(n);
        }
    }

    //129
    //129/10=12 9
    //12/10=1 2
    //2/10=0 2
    public static int getNext(int n){
        int sum = 0;
        while (n > 0) {
            sum = sum + ((n%10) * (n%10));
            n = n / 10;
        }
        return sum;
    }
}
